package com.ndood.spider.tmall.web;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.google.gson.Gson;
import com.ndood.core.util.excel.ExcelWriteHelper;
import com.ndood.core.util.excel.WriteRowMapper;
import com.ndood.spider.tmall.bean.TmallGoods;
import com.ndood.spider.tmall.dao.TmallGoodsDao;

/**
 * 爬虫控制器
 */
@Controller
@RequestMapping("/tmall")
public class TmallExportController {

	@Autowired
	private TmallGoodsDao tmallGoodsDao;
	
	/**
	 * 按店铺导出商品到excel文件
	 */
	@RequestMapping("/export")
	public String export(String shopname) throws Exception {
		// Step1: 查询出某店铺的所有商品
		List<TmallGoods> list = tmallGoodsDao.getListByShopNameOrderByCat(shopname);
		
		// Step2: 制作表格
		//标题  
        List<String> titleList = new ArrayList<String>();  
        titleList.add("商品ID");  
        titleList.add("店铺名称");  
        titleList.add("商品名称");  
        titleList.add("网址"); 
        titleList.add("价格");  
        titleList.add("分类");  
        titleList.add("分类路径");  
        titleList.add("销量");  
        titleList.add("评论数");  
        titleList.add("商品图片");  
        titleList.add("商品颜色");
        titleList.add("品牌");
        titleList.add("商品描述");
        // 参数表头
        for (TmallGoods tmallGoods : list) {
        	Map<String, String> params = tmallGoods.getParams();
        	for(Iterator<String> it= params.keySet().iterator();it.hasNext();){
        		String key = it.next();
        		if(titleList.contains(key)){
        			continue;
        		}
        		titleList.add(key);
        	}
        }

        //内容  
        List<Map<String,Object>> dataList = new ArrayList<Map<String,Object>>();  
        for (TmallGoods goods : list) {
        	Map<String, Object> map = new HashMap<String, Object>(); 
        	map.put("id", goods.getId());
        	map.put("shopName", goods.getShopName());
        	map.put("name", goods.getName());
        	map.put("url", goods.getUrl());
        	map.put("price", goods.getPrice());
        	map.put("cat", goods.getCat());
        	map.put("catPath", goods.getCatPath());
        	map.put("salesCount", goods.getSalesCount());
        	map.put("commentsCount", goods.getCommentsCount());
        	map.put("imgUrls", goods.getImgUrls());
        	map.put("colorUrls", goods.getColorUrls());
        	map.put("brand", goods.getBrand());
        	map.put("descUrls", goods.getDescUrls());
        	Map<String, String> params = goods.getParams();
        	for(Iterator<String> it= params.keySet().iterator();it.hasNext();){
        		String key = it.next();
        		map.put(key, params.get(key));
        	}
        	dataList.add(map);
        }
        
        // 导出表格为文件
        ExcelWriteHelper helper = new ExcelWriteHelper("E:/tmallGoods/"+shopname+".xls");
        helper.exportExcel("sheet1", titleList, dataList, new WriteRowMapper() {
			public List<String> handleData(Object data, List<String> titles){
				List<String> result = new ArrayList<String>();
				Map<String,Object> map = (Map<String,Object>)data;
				result.add((String)map.get("id"));
				result.add((String)map.get("shopName"));
				result.add((String)map.get("name"));
				result.add((String)map.get("url"));
				result.add((String)map.get("price"));
				result.add((String)map.get("cat"));
				result.add((String)map.get("catPath"));
				result.add((String)map.get("salesCount"));
				result.add((String)map.get("commentsCount"));
				Gson gson = new Gson();
				result.add(gson.toJson(map.get("imgUrls")));
				result.add(gson.toJson(map.get("colorUrls")));
				result.add((String)map.get("brand"));
				result.add(gson.toJson(map.get("descUrls")));
				
				// 从第13个属性开始，按顺序遍历title，如果不存在该字段则设为空值
				for (int i = 13; i < titles.size(); i++) {
					String key = titles.get(i);
					if(map.get(key)==null){
						result.add("");
						continue;
					}
					// 如果存在该字段则添加字段
					result.add(map.get(key).toString());
				}
				return result;
			}
		});
        return "ok";
	}
	
}
